﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Beauty.Model;
using Beauty.BLL;
using WebUI.Areas.Admin.Models;

namespace WebUI.Areas.Admin.Controllers
{
    public class LoginController : Controller
    {
        // GET: Admin/Login
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult CheckLogin(string username, string password)
        {
            bool isAutoLogin = true;
            if (string.IsNullOrEmpty(Request["auto_login"]))
            {
                isAutoLogin = false;
            }
            List<user> users = bllUser.GetModelList("UName='" + username+"'");
            if (users.Count < 1)
            {
                return Json(new { erro_code = 1, msg = "用户名不存在" });
            }
            else if (!users[0].UPass.Equals(password))
            {
                return Json(new { erro_code = 2, msg = "密码输入错误" });
            }

            Session[AdminDataConfig.UserSessionName] = users[0];
            if (isAutoLogin)
            {
                HttpCookie cookies = new HttpCookie(AdminDataConfig.AutoLoginCookie);
                cookies.Values.Add("name", username);
                cookies.Values.Add("pass", password);
                cookies.Expires = DateTime.Now.AddDays(7);
                Response.AppendCookie(cookies);
            }
            return Json(new { erro_code = 0, msg = "success" });
        }

        userBLL bllUser = new userBLL();
    }
}